package weaver.interfaces.cese.duss.service.impl;

import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.cese.duss.ceseutil.WorkflowCreateHandler;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-11-25
 * @Description: 生成供应商认证待办流程接口
 * @ModifyLog:
 **/
@WebServlet("/supplier/certificat")
public class SupplierCertificatServiceImpl extends HttpServlet {
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        super.doPost( req, res );
    }

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String gyslx = Util.null2String( req.getParameter( "gyslx" ) );
        String gyszh = Util.null2String( req.getParameter( "gyszh" ) );
        BaseBean baseBean = new BaseBean();
        baseBean.writeLog( "-----------SupplierCertificatServiceImpl------------" );
        String creatorId = "";//创建人
        String workflowId = "";//流程ID
        String requestName = "";//流程名称
        String IsNextFlow = "0";//是否提交下一节点
        String WULIAO = "0";//证书类型-物料
        String GONGCHENG = "1";//证书类型-工程
        String FUWU = "2";//证书类型-服务
        String workflowID_wl = baseBean.getPropValue( "fk_srm_srm", "workflowID_wl" );//物料流程ID
        String workflowID_fw = baseBean.getPropValue( "fk_srm_srm", "workflowID_fw" );//服务流程ID
        String workflowID_gc = baseBean.getPropValue( "fk_srm_srm", "workflowID_gc" );//工程流程ID

        Map<String, String> requestMainMap = new HashMap<>();
        WorkflowCreateHandler handler = new WorkflowCreateHandler();
        if (WULIAO.equals( gyslx )) {  //物料供应商认证表单
            Map<String, String> resultMap = loginid2ID(gyszh);
            creatorId = resultMap.get( "id" );
            workflowId = workflowID_wl;
            requestName = "供应商管理-物料供应商认证表单-" + DateUtil.getDateTime() + "-" + resultMap.get( "lastname" );
            handler.WorkflowCreateByRequestMap( creatorId, workflowId, requestName, IsNextFlow, requestMainMap );

        } else if (FUWU.equals( gyslx )) { //服务供应商认证表单
            Map<String, String> resultMap = loginid2ID( gyszh);
            creatorId = resultMap.get( "id" );
            workflowId = workflowID_fw;
            requestName = "供应商管理-服务供应商认证表单-" + DateUtil.getDateTime() + "-" + resultMap.get( "lastname" );
            handler.WorkflowCreateByRequestMap( creatorId, workflowId, requestName, IsNextFlow, requestMainMap );

        } else if (GONGCHENG.equals( gyslx )) { //工程供应商认证表单
            Map<String, String> resultMap = loginid2ID(gyszh);
            creatorId = resultMap.get( "id" );
            workflowId = workflowID_gc;
            requestName = "供应商管理-工程供应商认证表单-" + DateUtil.getDateTime() + "-" + resultMap.get( "lastname" );
            handler.WorkflowCreateByRequestMap( creatorId, workflowId, requestName, IsNextFlow, requestMainMap );
        }

    }

    //人力资源loginid转id
    public static Map<String, String> loginid2ID(String loginid) {
        Map<String, String> returnMap = new HashMap<>();
        RecordSet rs = new RecordSet();
        String id = "";
        String lastname = "";
        rs.executeQuery( "select id,lastname from hrmresource where loginid='" + loginid + "'" );
        if (rs.next()) {
            id = Util.null2String( rs.getString( "id" ) );
            lastname = Util.null2String( rs.getString( "lastname" ) );
            returnMap.put( "id", id );
            returnMap.put( "lastname", lastname );
        }
        return returnMap;
    }
}
